package com.yeyks.commonReference.service.mattress.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.yeyks.common.exception.ServiceException;
import com.yeyks.commonReference.append.mattress.api.MattressApiCallProperties;
import com.yeyks.commonReference.append.mattress.api.MattressApiCallService;
import com.yeyks.commonReference.service.mattress.MattressApiService;
import com.yeyks.commonReference.service.mattress.MattressInfoService;
import com.yeyks.mattress.dal.domain.MattressInfo;
import com.yeyks.mattress.param.OrderInfoMattressMattressStatusParam;
import com.yeyks.mattress.vo.OrderInfoMattressMattressStatusVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.autoconfigure.condition.ConditionalOnBean;
import org.springframework.stereotype.Service;

/**
 * <p>
 * 共享床垫api业务类 服务实现类
 * </p>
 *
 * @author yun
 * @since 2019-07-16
 */
@Service
@ConditionalOnBean(MattressApiCallProperties.class)
public class MattressApiServiceImpl implements MattressApiService {

    @Autowired
    private MattressInfoService mattressInfoService;
    @Autowired
    private MattressApiCallService mattressApiCallService;

    @Override
    public OrderInfoMattressMattressStatusVo mattressStatus(OrderInfoMattressMattressStatusParam param) {
        if (null == param.getUserId()) {
            throw new ServiceException("用户不存在");
        }
        MattressInfo mattressInfo = mattressInfoService.getOne(new QueryWrapper<MattressInfo>()
                .eq(MattressInfo.IMEI_ID, param.getImeiId()).eq(MattressInfo.STATUS, 1));
        if (null == mattressInfo) {
            throw new ServiceException("床垫不存在");
        }
        //查询机器使用状态
        return mattressStatusOnCheck(param.getImeiId());
    }

    @Override
    public OrderInfoMattressMattressStatusVo mattressStatusOnCheck(String imeiId) {
        //查询机器在线状态
        boolean onlineStatus = mattressApiCallService.getOnlineStatus(imeiId);
        if (!onlineStatus) {
            throw new ServiceException("床垫不在线");
        }
        //查询机器使用状态
        //v1.0.1
//        String status = valueOperations.get(RedisKeyPreContent.MATTRESS_MECHANICAL_LOCK + imeiId);
//        if (StrUtil.isNotBlank(status)) {
//            switch (status) {
//                case "lock":
//                    return new OrderInfoMattressMattressStatusVo().setStatus(1);
//                case "locked":
//                    return new OrderInfoMattressMattressStatusVo().setStatus(2);
//                default:
//                    return new OrderInfoMattressMattressStatusVo().setStatus(0);
//            }
//        }
        return new OrderInfoMattressMattressStatusVo().setStatus(0);
    }

}
